/*
 * Created by JFormDesigner on Mon Apr 10 17:10:53 CST 2023
 */

package com.canteen.gui.customerform;

import com.canteen.entity.CanteenCustomer;
import com.canteen.mapper.CanteenCustomerMapper;
import com.canteen.util.MybatisUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.session.SqlSession;

import java.awt.*;
import java.awt.event.*;
import java.util.Date;
import javax.swing.*;

/**
 * @author s303teacher
 */
@Slf4j
public class CustomerForm extends JFrame {
    public CustomerForm() {
        initComponents();
    }

    private void saveCustomer(ActionEvent e) {
        // TODO add your code here
        //创建一个客户对象
        CanteenCustomer customer = new CanteenCustomer();
        customer.setCustomerId("10012");
        customer.setCustomerName(customerNameInput.getText());
        customer.setCustomerMobile(customerPhoneInput.getText());
        customer.setCustomerAddress(customerAddressInput.getText());
        customer.setCustomerPostscript(postscriptInput.getText());
        customer.setCreateTime(new Date(System.currentTimeMillis()));
        customer.setUpdateTime(new Date(System.currentTimeMillis()));

        //将对象中的信息存储到数据库中
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        try{
            //方式一：getMapper
            CanteenCustomerMapper mapper = sqlSession.getMapper(CanteenCustomerMapper.class);
            int objectInsert = mapper.insert(customer);
            sqlSession.commit();
            System.out.println(objectInsert);
        }catch (Exception exception){
            sqlSession.rollback();
            log.error("can not insert data",e);
        }
    }

    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents  @formatter:off
        customerNameLabel = new JLabel();
        customerNameInput = new JTextField();
        customerPhoneLabel = new JLabel();
        customerPhoneInput = new JTextField();
        customerAddressLabel = new JLabel();
        customerAddressInput = new JTextField();
        postscriptLabel = new JLabel();
        postscriptInput = new JTextField();
        saveButton = new JButton();
        cancelButton = new JButton();

        //======== this ========
        setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
        setResizable(false);
        setTitle("\u987e\u5ba2\u5f55\u5165");
        var contentPane = getContentPane();
        contentPane.setLayout(null);

        //---- customerNameLabel ----
        customerNameLabel.setText("\u987e\u5ba2\u59d3\u540d\uff1a");
        contentPane.add(customerNameLabel);
        customerNameLabel.setBounds(50, 35, 65, 30);
        contentPane.add(customerNameInput);
        customerNameInput.setBounds(115, 35, 130, 30);

        //---- customerPhoneLabel ----
        customerPhoneLabel.setText("\u987e\u5ba2\u624b\u673a\uff1a");
        contentPane.add(customerPhoneLabel);
        customerPhoneLabel.setBounds(50, 75, 65, 30);
        contentPane.add(customerPhoneInput);
        customerPhoneInput.setBounds(115, 75, 130, 30);

        //---- customerAddressLabel ----
        customerAddressLabel.setText("\u987e\u5ba2\u5730\u5740\uff1a");
        contentPane.add(customerAddressLabel);
        customerAddressLabel.setBounds(50, 120, 65, 30);
        contentPane.add(customerAddressInput);
        customerAddressInput.setBounds(115, 120, 235, 30);

        //---- postscriptLabel ----
        postscriptLabel.setText("\u5907     \u6ce8\uff1a");
        contentPane.add(postscriptLabel);
        postscriptLabel.setBounds(45, 165, 70, 30);
        contentPane.add(postscriptInput);
        postscriptInput.setBounds(115, 165, 235, 30);

        //---- saveButton ----
        saveButton.setText("\u4fdd\u5b58");
        saveButton.addActionListener(e -> saveCustomer(e));
        contentPane.add(saveButton);
        saveButton.setBounds(55, 225, 110, saveButton.getPreferredSize().height);

        //---- cancelButton ----
        cancelButton.setText("\u53d6\u6d88");
        contentPane.add(cancelButton);
        cancelButton.setBounds(240, 225, 110, 28);

        {
            // compute preferred size
            Dimension preferredSize = new Dimension();
            for(int i = 0; i < contentPane.getComponentCount(); i++) {
                Rectangle bounds = contentPane.getComponent(i).getBounds();
                preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
            }
            Insets insets = contentPane.getInsets();
            preferredSize.width += insets.right;
            preferredSize.height += insets.bottom;
            contentPane.setMinimumSize(preferredSize);
            contentPane.setPreferredSize(preferredSize);
        }
        setSize(400, 330);
        setLocationRelativeTo(getOwner());
        // JFormDesigner - End of component initialization  //GEN-END:initComponents  @formatter:on
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables  @formatter:off
    private JLabel customerNameLabel;
    private JTextField customerNameInput;
    private JLabel customerPhoneLabel;
    private JTextField customerPhoneInput;
    private JLabel customerAddressLabel;
    private JTextField customerAddressInput;
    private JLabel postscriptLabel;
    private JTextField postscriptInput;
    private JButton saveButton;
    private JButton cancelButton;
    // JFormDesigner - End of variables declaration  //GEN-END:variables  @formatter:on
}
